package com.rc.evcharger.rabbit;

import com.rc.evcharger.common.config.MqCommandConfig;
import com.rc.evcharger.common.config.RabbitListenerConstant;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Create by Yang on 2021/7/29
 * 注册交换机
 * 地址   https://blog.csdn.net/qq_25235871/article/details/106022880
 */
@Configuration
public class ExchangeConfig {

    @Autowired
    private MqCommandConfig mqCommandConfig;

    public static final int DEFAULT_CONCURRENT = 1;
    //@Autowired
    //private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;

    /**
     * 注入name为'x-delayed-message'customExchange,默认名字就是空字符串，可以不注入
     */
    @Bean
    public CustomExchange customExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitListenerConstant.EXCHANGE_COMMANDEXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    @Qualifier("secondSaasExchange")
    public CustomExchange secondSaasExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitListenerConstant.EXCHANGE_SAASEXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 注入name为'x-delayed-message'customExchange,默认名字就是空字符串，可以不注入
     */
//    @Bean
//    public CustomExchange delayedExchange() {
//        Map<String, Object> args = new HashMap<>();
//        args.put("x-delayed-type", "direct");
//        return new CustomExchange("delayedExchange", "x-delayed-message", true, false, args);
//    }


    @Bean("customContainerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                 ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(DEFAULT_CONCURRENT);
        factory.setMaxConcurrentConsumers(DEFAULT_CONCURRENT);
        factory.setPrefetchCount(250);
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    //默认自动确认模式
    //@Bean
    //@ConditionalOnClass
    //public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(CachingConnectionFactory connectionFactory) {
    //    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    //    factory.setConnectionFactory(connectionFactory);
    //    factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
    //    //factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    //    return factory;
    //}

    //心跳手动确认模式
    //@Bean
    //@ConditionalOnClass
    //public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactoryManual(CachingConnectionFactory connectionFactory) {
    //    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    //    factory.setConnectionFactory(connectionFactory);
    //    //factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
    //    factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    //    return factory;
    //}

    //@Bean
    //public MessageListenerContainer messageListenerContainer() {
    //    String id = "";
    //    if (mqCommandConfig.getApplicationName().endsWith("1")) {
    //        id = RabbitListenerConstant.DOMAIN1;
    //    } else if (mqCommandConfig.getApplicationName().endsWith("2")) {
    //        id = RabbitListenerConstant.DOMAIN2;
    //    } else if (mqCommandConfig.getApplicationName().endsWith("3")) {
    //        id = RabbitListenerConstant.DOMAIN3;
    //    } else if (mqCommandConfig.getApplicationName().endsWith("4")) {
    //        id = RabbitListenerConstant.DOMAIN4;
    //    } else {
    //        id = RabbitListenerConstant.DOMAIN5;
    //    }
    //    //1.获取监听的容器
    //    MessageListenerContainer container = rabbitListenerEndpointRegistry.getListenerContainer(id);
    //    //2.开启容器
    //    if (!container.isRunning()) {
    //        container.start();
    //        System.out.println("开启容器,id =" + id);
    //    }
    //    return container;
    //}

    //
    ///**
    // *注入name为'direct'的DirectExchange,默认名字就是空字符串，可以不注入
    // */
    //@Bean
    //DirectExchange directExchange() {
    //    return new DirectExchange("commandExchange");
    //}
    //
    /////**
    //// *注入name为exchange的TopicExchange
    //// */
    //@Bean
    //TopicExchange exchange() {
    //    return new TopicExchange("exchange");
    //}
    //
    //
    /////**
    //// * 注入name为fanoutExchange的FanoutExchange
    //// */
    //@Bean
    //FanoutExchange fanoutExchange() {
    //    return new FanoutExchange("fanoutExchange");
    //}
}
